package com.wl.application.services.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.application.context.LoginLocalThread;
import com.wl.application.common.all.Result;
import com.wl.application.common.all.ResultEnum;
import com.wl.application.common.childException.NoPowerException;
import com.wl.application.common.untils.HttpUtil;
import com.wl.application.entity.*;
import com.wl.application.entity.vo.AllotVo;
import com.wl.application.mapper.AllotMapper;
import com.wl.application.mapper.UserMapper;
import com.wl.application.services.AllotService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.AntPathMatcher;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/23
 * @description:
 */
@Service
public class AllotServiceImpl extends ServiceImpl<AllotMapper, UserRole> implements AllotService {
    @Resource
    private AllotMapper allotMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean addRoleToUser(Integer rid, List<Integer> users) {
        allotMapper.addRoleToUser(rid,users);
        return true;
    }

    @Override
    public boolean checkPower() {
        //在本地线程中获取当前用户的权限
        LoginUser local = LoginLocalThread.getLocal();
        Set<String> powerUrls = local.getPowerUrls();
        AntPathMatcher matcher=new AntPathMatcher();
        String url= HttpUtil.getUrl().replace("/api","");

//        System.out.println(url);
//        powerUrls.stream().collect(Collectors.toSet()).forEach((e)-> System.out.println(e));
        for (String powerUrl : powerUrls) {
            if(powerUrl!=null){
                boolean match = matcher.match(powerUrl,url);
                if(match){
                    return true;
                }
            }
        }
        throw  new NoPowerException(ResultEnum.NO_POWER.getCode(),ResultEnum.NO_POWER.getMsg());
    }

    @Override
    public Set<String>
    getAllPowerByUid(Integer uid) {
        //1.联表查询该用户下的所有权限
        Set<String> PowerUrls = allotMapper.getRole(uid).stream().map((e)->e.getUrl()).collect(Collectors.toSet());
        return PowerUrls;
    }



    @Override
    public boolean removeUserAndUr(List<Integer> ids) {
        allotMapper.removeUserAndUr(ids);
        return true;
    }

    @Override
    public Page<User> noAllotPower(AllotVo allotVo) {
        Page<User> page=new Page<>(allotVo.getCurrent(),allotVo.getSize());
        Page<User> list=userMapper.noAllotPower(page,allotVo);
        return list;
    }

    @Override
    public Page<User> getUserListByRid(AllotVo allotVo) {
        Page<User> page = new Page<>(allotVo.getCurrent(), allotVo.getSize());
        Page<User> userListByRidPage = allotMapper.getUserListByRidPage(page, allotVo);
        return userListByRidPage;
    }

    @Override
    @Transactional//开启手动事务
    public boolean addPower(AllotVo allotVo) {

        //1.删除该角色下的所有的权限
        allotMapper.deleteAllPowerForId(allotVo);
        if (allotVo.getPowerIdList() == null||allotVo.getPowerIdList().size()==0) {
            return true;
        }
        //2.将当前的权限集合添加给本对象
        allotMapper.addPowerForId(allotVo);
        return true;
    }

    @Override
    public Result PowerInfo(Integer rid) {
        return Result.ok(allotMapper.PowerInfo(rid));
    }
}
